﻿using System;
using System.Text.RegularExpressions;
using System.Data;
using System.Diagnostics; 

namespace Utils
    {
    /// <summary>
    /// Summary description for Utils.Debugger
    /// </summary>
    public class Debugger
        {
        public Debugger ( )
            {
            //
            // TODO: Add constructor logic here
            //
            }
        /// <summary>
        /// This method outputs on values of the passed dataset objects for easier debugging
        /// </summary>
        /// <param name="global.msg"> the string passed containng usual info</param>
        /// <param name="ds"> the dataset , which content one should debug </param>
        /// call it this way  //debugOkDataSet (  System.Reflection.MethodInfo.GetCurrentMethod().Name() ,  , ref ds );         
        public static void DebugDataSet ( string msg, ref System.Data.DataSet ds )
            {

            WriteIf ( "===================================================" + msg + " START " );
            if (ds != null)
                {
                WriteIf ( msg );
                foreach (System.Data.DataTable dt in ds.Tables)
                    {
                    WriteIf ( "================= My TableName is  " +
                    dt.TableName + " ========================= START" );
                    int colNumberInRow = 0;
                    foreach (System.Data.DataColumn dc in dt.Columns)
                        {
                        System.Diagnostics.Debug.Write ( " | " );
                        System.Diagnostics.Debug.Write ( " |" + colNumberInRow + "| " );
                        System.Diagnostics.Debug.Write ( dc.ColumnName + " | " );
                        colNumberInRow++;
                        } //eof foreach (DataColumn dc in dt.Columns)
                    int rowNum = 0;
                    foreach (System.Data.DataRow dr in dt.Rows)
                        {
                        System.Diagnostics.Debug.Write ( "\n row " + rowNum + " --- " );
                        int colNumber = 0;
                        foreach (System.Data.DataColumn dc in dt.Columns)
                            {
                            System.Diagnostics.Debug.Write ( " |" + colNumber + "| " );
                            System.Diagnostics.Debug.Write ( dr [ dc ].ToString ( ) + " " );
                            colNumber++;
                            } //eof foreach (DataColumn dc in dt.Columns)
                        rowNum++;
                        }	//eof foreach (DataRow dr in dt.Rows)
                    System.Diagnostics.Debug.Write ( " \n" );
                    WriteIf ( "================= Table " +
            dt.TableName + " ========================= END" );
                    WriteIf ( "===================================================" + msg + " END " );
                    }	//eof foreach (DataTable dt in ds.Tables)
                } //eof if ds !=null 
            else
                {
                WriteIf ( "NULL DataSet object passed for debugging !!!" ); 
             }
            } //eof method 



				public static void DebugDataTable ( string msg , ref DataTable dt )
				{
						if (dt == null)
						{
								WriteIf ( "DebugDataTable I have been passed a null DataTable " );
								return; 
						} 
										WriteIf ( "================= My TableName is  " +
                    dt.TableName + " ========================= START" );
										int colNumberInRow = 0;
										foreach (System.Data.DataColumn dc in dt.Columns)
										{
												System.Diagnostics.Debug.Write ( " | " );
												System.Diagnostics.Debug.Write ( " |" + colNumberInRow + "| " );
												System.Diagnostics.Debug.Write ( dc.ColumnName + " | " );
												colNumberInRow++;
										} //eof foreach (DataColumn dc in dt.Columns)
										int rowNum = 0;
										foreach (System.Data.DataRow dr in dt.Rows)
										{
												System.Diagnostics.Debug.Write ( "\n row " + rowNum + " --- " );
												int colNumber = 0;
												foreach (System.Data.DataColumn dc in dt.Columns)
												{
														System.Diagnostics.Debug.Write ( " |" + colNumber + "| " );
														System.Diagnostics.Debug.Write ( dr [ dc ].ToString () + " " );
														colNumber++;
												} //eof foreach (DataColumn dc in dt.Columns)
												rowNum++;
										}	//eof foreach (DataRow dr in dt.Rows)
										System.Diagnostics.Debug.Write ( " \n" );
										WriteIf ( "================= Table " +
				            dt.TableName + " ========================= END" );
										WriteIf ( "===================================================" + msg + " END " );

				} //eof method 

				/// <summary>
				/// This is the main debugging method, it outputs the debugging messages echoed in the code 
				/// according to the output , which is set in the web.config file, read the comments for each case 
				/// </summary>
				/// <param name="msg"></param>
				public static void WriteIf ( string msg )
				{ 
					
					
					//TODO: FIND OUT ABOUT e.Message + e.StackTrace from Bromberg eggcafe
						int output = System.Convert.ToInt16 (
								System.Configuration.ConfigurationSettings.AppSettings ["DebugOutput"] );
						//0 - do not debug anything just run the code 
						switch ( output )
						{
							//do not debug anything	
							case 0:
										msg = String.Empty;
										break;
								//1 - output to debug window in Visual Studio 		
								case 1:
									System.Diagnostics.Debug.WriteIf( System.Convert.ToBoolean
												( System.Configuration.ConfigurationSettings.AppSettings [ "Debugging" ] ) ,
										DateTime.Now.ToString ( "yyyy:MM:dd -- hh:mm:ss.fff --- " ) + msg + "\n" );
										break;

								//2 -- output to the error label in the master 
								case 2:
										string previousMsg = System.Convert.ToString (
														System.Web.HttpContext.Current.Session [ "global.DebugMsg" ] );
										System.Web.HttpContext.Current.Session [ "global.DebugMsg" ] = previousMsg + 
										DateTime.Now.ToString ( "yyyy:MM:dd -- hh:mm:ss.fff --- " ) + msg + "\n </br>";
										break;
							//output both to debug window and error label 
							case 3:
								string previousMsg1 = System.Convert.ToString (
												System.Web.HttpContext.Current.Session ["global.DebugMsg"] );
								System.Web.HttpContext.Current.Session ["global.DebugMsg"] = previousMsg1 + 
										DateTime.Now.ToString ( "yyyy:MM:dd -- hh:mm:ss.fff --- " ) + msg + "\n";
								System.Diagnostics.Debug.WriteIf ( System.Convert.ToBoolean
										( System.Configuration.ConfigurationSettings.AppSettings ["Debugging"] ),
								DateTime.Now.ToString ( "yyyy:MM:dd -- hh:mm:ss.fff --- " ) + msg + "\n </br>" );
								break;
								//TODO: implement case when debugging goes to database 
						} //eof switch 

				} //eof method 






        public static void IsNull ( object o )
            {
            if (o == null)
                {
                WriteIf ( o.ToString () + " IS null " );
                return;
                }
            else
                {
                WriteIf ( o.ToString ( ) + " IS NOT null" );
                }
        } //eof IsNull

				

			
			
			public static void DebugPage ( System.Web.UI.Page page )
{

string baseDirLocal = System.Web.HttpContext.Current.Server.MapPath ( "~" );
Regex Remover = new Regex ( @"^.*(\\|\/)(.*)$",
RegexOptions.IgnoreCase | RegexOptions.Compiled );
string bareDir = Remover.Replace (  baseDirLocal , "$2" );


string strToRemoveAtEnd= System.Web.HttpContext.Current.Request.Url.AbsolutePath;
char [] charsToRemoveAtEnd = strToRemoveAtEnd.ToCharArray ();
string baseDir = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
baseDir = baseDir.TrimEnd ( charsToRemoveAtEnd );
baseDir = baseDir + "/" + bareDir + "/";

WriteIf ( "Now I should get the www.myhost.com:80/baseDir - baseDir , but I get " +
	baseDir );

WriteIf("The basedir of the project locally is is" + 
	" HttpContext.Current.Server.MapPath(\"~/\");" +
	System.Web.HttpContext.Current.Server.MapPath ( "~" ) );

WriteIf ( "The basedir of the project locally is " + 
	"AppDomain.CurrentDomain.BaseDirectory;" + 
	AppDomain.CurrentDomain.BaseDirectory);

WriteIf ( "The virtual path of this page " + 
	"( System.Web.HttpContext.Current.Request.PathInfo) is " + 
	System.Web.HttpContext.Current.Request.PathInfo ) ;

WriteIf ( "System.IO.Path.GetFileName ( System.Web.HttpContext.Current.Request.Url.AbsolutePath ) -- " + 
		System.IO.Path.GetFileName ( System.Web.HttpContext.Current.Request.Url.AbsolutePath ) );
WriteIf ( "System.Web.HttpContext.Current.Request.Url.AbsolutePath -- " + 
																		System.Web.HttpContext.Current.Request.Url.AbsolutePath );
WriteIf ( "System.Web.HttpContext.Current.Request.Url.AbsoluteUri -- " + 
																		System.Web.HttpContext.Current.Request.Url.AbsoluteUri );
WriteIf ( "System.Web.HttpContext.Current.Request.Url.DnsSafeHost -- " + 
																		System.Web.HttpContext.Current.Request.Url.DnsSafeHost );
WriteIf ( "System.Web.HttpContext.Current.Request.Url.Fragment -- " + 
																		System.Web.HttpContext.Current.Request.Url.Fragment );
WriteIf ( "System.Web.HttpContext.Current.Request.Url.Host -- " + 
																		System.Web.HttpContext.Current.Request.Url.Host ) ; 
WriteIf ( "System.Web.HttpContext.Current.Request.Url.HostNameType -- " + 
																		System.Web.HttpContext.Current.Request.Url.HostNameType ) ;
WriteIf ( "System.Web.HttpContext.Current.Request.Url.LocalPath -- " + 
																		System.Web.HttpContext.Current.Request.Url.LocalPath ) ;
WriteIf ( "System.Web.HttpContext.Current.Request.Url.OriginalString -- " + 
																		System.Web.HttpContext.Current.Request.Url.OriginalString ) ;
WriteIf ( "System.Web.HttpContext.Current.Request.Url.PathAndQuery -- " + 
																		System.Web.HttpContext.Current.Request.Url.PathAndQuery ) ;
WriteIf ( "System.Web.HttpContext.Current.Request.Url.Scheme -- " + 
																		System.Web.HttpContext.Current.Request.Url.Scheme ) ;
WriteIf ( "System.Web.HttpContext.Current.Request.Url.Segments.ToString () -- " + 
																		System.Web.HttpContext.Current.Request.Url.Segments.ToString () );
WriteIf ( "System.Web.HttpContext.Current.Request.Url.UserInfo -- " + 
																		System.Web.HttpContext.Current.Request.Url.UserInfo );
WriteIf ( "System.Web.HttpContext.Current.Request.ServerVariables[ \"HTTP_COOKIE\" ] is " + 
		System.Web.HttpContext.Current.Request.ServerVariables [ "HTTP_COOKIE" ] );

} //eof DebugPage


				} //eof public  class Utils.Debugger 
    } //eof namespace Utils

